package com.prawn.trace.record.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import com.prawn.trace.record.dao.InvestDao;
import com.prawn.trace.record.pojo.Invest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;
import util.NullAttributeUtils;



/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class InvestService {

	@Autowired
	private InvestDao investDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Invest> findAll() {
		return investDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Invest> findSearch(Map whereMap, int page, int size) {
		Specification<Invest> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return investDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Invest> findSearch(Map whereMap) {
		Specification<Invest> specification = createSpecification(whereMap);
		return investDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Invest findById(String id) {
		return investDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param invest
	 */
	public void add(Invest invest) {
		invest.setId( idWorker.nextId()+"" );
		investDao.save(invest);
	}

	/**
	 * 修改
	 * @param invest
	 */
	public void update(Invest invest) {
		Invest oldInvest =investDao.findById(invest.getId()).get();
		BeanUtils.copyProperties(invest,oldInvest, NullAttributeUtils.getNullPropertyNames(invest));


		investDao.save(oldInvest);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		investDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Invest> createSpecification(Map searchMap) {

		return new Specification<Invest>() {

			@Override
			public Predicate toPredicate(Root<Invest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 投料批次编号
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 投料名称
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 原料图片
                if (searchMap.get("pic")!=null && !"".equals(searchMap.get("pic"))) {
                	predicateList.add(cb.like(root.get("pic").as(String.class), "%"+(String)searchMap.get("pic")+"%"));
                }
                // 保质期
                if (searchMap.get("qualityPeriod")!=null && !"".equals(searchMap.get("qualityPeriod"))) {
                	predicateList.add(cb.like(root.get("qualityPeriod").as(String.class), "%"+(String)searchMap.get("qualityPeriod")+"%"));
                }
                // 检验人ID
                if (searchMap.get("checkPerson")!=null && !"".equals(searchMap.get("checkPerson"))) {
                	predicateList.add(cb.like(root.get("checkPerson").as(String.class), "%"+(String)searchMap.get("checkPerson")+"%"));
                }
                // 检验结果
                if (searchMap.get("checkResult")!=null && !"".equals(searchMap.get("checkResult"))) {
                	predicateList.add(cb.like(root.get("checkResult").as(String.class), "%"+(String)searchMap.get("checkResult")+"%"));
                }
                // 供应商名称
                if (searchMap.get("supplier")!=null && !"".equals(searchMap.get("supplier"))) {
                	predicateList.add(cb.like(root.get("supplier").as(String.class), "%"+(String)searchMap.get("supplier")+"%"));
                }
                // 供应商地址
                if (searchMap.get("supplierAddr")!=null && !"".equals(searchMap.get("supplierAddr"))) {
                	predicateList.add(cb.like(root.get("supplierAddr").as(String.class), "%"+(String)searchMap.get("supplierAddr")+"%"));
                }
                // 供应商电话
                if (searchMap.get("supplierPhone")!=null && !"".equals(searchMap.get("supplierPhone"))) {
                	predicateList.add(cb.like(root.get("supplierPhone").as(String.class), "%"+(String)searchMap.get("supplierPhone")+"%"));
                }
                // 供应商许可证编号
                if (searchMap.get("supplierLicence")!=null && !"".equals(searchMap.get("supplierLicence"))) {
                	predicateList.add(cb.like(root.get("supplierLicence").as(String.class), "%"+(String)searchMap.get("supplierLicence")+"%"));
                }
                // 投料溯源接口(没有就为空)
                if (searchMap.get("traceNo")!=null && !"".equals(searchMap.get("traceNo"))) {
                	predicateList.add(cb.like(root.get("traceNo").as(String.class), "%"+(String)searchMap.get("traceNo")+"%"));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.equal(root.get("createBy").as(String.class), (String)searchMap.get("createBy")));
                }
                // 批次ID
                if (searchMap.get("produceBatchId")!=null && !"".equals(searchMap.get("produceBatchId"))) {
                	predicateList.add(cb.like(root.get("produceBatchId").as(String.class), "%"+(String)searchMap.get("produceBatchId")+"%"));
                }
                // 生产流程ID
                if (searchMap.get("processId")!=null && !"".equals(searchMap.get("processId"))) {
                	predicateList.add(cb.like(root.get("processId").as(String.class), "%"+(String)searchMap.get("processId")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
